package cn.xioabai.common.utility;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.event.SyncReadListener;
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
import com.alibaba.excel.read.listener.ReadListener;
import com.alibaba.excel.support.ExcelTypeEnum;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

/**
 * 表格导入 支持格式 (vsc,xls,xlsx)
 *
 * @Author yangdaji
 * @Date 2022/8/29 0029
 * @PackageName com.supconit.digital.twins.poi
 * @ClassName ExcelImportUtils
 */
@Slf4j
public class ExcelImportUtils {

    /**
     * 构建表对象
     *
     * @param inputStream  输入流
     * @param head         注释类配置
     * @param excelType    读取类型
     * @param readListener 监听读取结果
     * @return 表对象
     */
    public static ExcelReaderBuilder builderSheet(InputStream inputStream
            , Class<?> head, Object excelType, ReadListener readListener) {

        ExcelTypeEnum typeEnum = null;
        if (excelType instanceof String) {
            typeEnum = ExcelTypeEnum.valueOf(excelType.toString());
        } else if (excelType instanceof ExcelTypeEnum) {
            typeEnum = (ExcelTypeEnum) excelType;
        }
        return EasyExcel.read(inputStream, head, readListener)
                .excelType(typeEnum);
    }

    /**
     * 获取输入流
     *
     * @param stream 适配File,String类型
     * @return 输入流对象
     */
    public static InputStream inputStream(Object stream) {
        InputStream inputStream = null;
        Path path = null;
        if (stream instanceof File) {
            path = Paths.get(((File) stream).getPath());
        } else if (stream instanceof String) {
            path = Paths.get(stream.toString());
        }

        try {
            assert path != null;
            inputStream = Files.newInputStream(path);
        } catch (IOException e) {
            log.error("创建输入流失败: {}", e.getLocalizedMessage());
        }
        return inputStream;
    }

    /**
     * 创建同步读取监听
     *
     * @return
     */
    public static SyncReadListener createReadListener() {
        return new SyncReadListener();
    }


    ///////////////// csv 文件

    /**
     * 导出表 格式(csv)
     *
     * @param path         读取文件位置
     * @param head         配置类型
     * @param readListener 监听读取结果
     */
    public static void importExcelCsv(String path, Class<?> head, ReadListener readListener) {
        importExcelCsv(Paths.get(path).toFile(), head, readListener);
    }

    public static void importExcelCsv(File file, Class<?> head, ReadListener readListener) {
        importExcelCsv(inputStream(file), head, readListener);
    }

    public static void importExcelCsv(InputStream inputStream, Class<?> head, ReadListener readListener) {
        builderSheet(inputStream, head, ExcelTypeEnum.CSV, readListener).doReadAll();
    }


    ///////////////// xls 文件

    /**
     * 导出表 格式(xls)
     *
     * @param path         读取文件位置
     * @param head         配置类型
     * @param readListener 监听读取结果
     */
    public static void importExcelXls(String path, Class<?> head, ReadListener readListener) {
        importExcelXls(Paths.get(path).toFile(), head, readListener);
    }

    public static void importExcelXls(File file, Class<?> head, ReadListener readListener) {
        importExcelXls(inputStream(file), head, readListener);
    }

    public static void importExcelXls(InputStream inputStream, Class<?> head, ReadListener readListener) {
        builderSheet(inputStream, head, ExcelTypeEnum.XLS, readListener).doReadAll();
    }


    ///////// xlsx 文件

    /**
     * 导出表 格式(xlsx)
     *
     * @param path         读取文件位置
     * @param head         配置类型
     * @param readListener 导出数据
     */
    public static void importExcelXlsx(String path, Class<?> head, ReadListener readListener) {
        importExcelXlsx(Paths.get(path).toFile(), head, readListener);
    }

    public static void importExcelXlsx(File file, Class<?> head, ReadListener readListener) {
        importExcelXlsx(inputStream(file), head, readListener);
    }

    public static void importExcelXlsx(InputStream inputStream, Class<?> head, ReadListener readListener) {
        builderSheet(inputStream, head, ExcelTypeEnum.XLSX, readListener).doReadAll();
    }

}
